package com.smooth.sys.pc.controller;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.PolicyConditions;
import com.smooth.common.site.common.rsp.ReturnResult;
import com.smooth.sys.pc.vo.rsp.AliPicRsp;

@Controller
@RequestMapping(value = "/pc")
public class AliImgController {
    private static String endpoint = "http://oss-cn-zhangjiakou.aliyuncs.com";
    private static String host = "http://activity-20190118.oss-cn-zhangjiakou.aliyuncs.com";
    // accessKeyId和accessKeySecret是OSS的访问密钥，您可以在控制台上创建和查看，
    // 创建和查看访问密钥的链接地址是：https://ak-console.aliyun.com/#/。
    // 注意：accessKeyId和accessKeySecret前后都没有空格，从控制台复制时请检查并去除多余的空格。
    private static String accessKeyId = "LTAI1jtVjWXQzYbK";
    private static String accessKeySecret = "EJRvNQrIXntBetzbckjrYQZjq53dxL";

    // Bucket用来管理所存储Object的存储空间，详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
    // Bucket命名规范如下：只能包括小写字母，数字和短横线（-），必须以小写字母或者数字开头，长度必须在3-63字节之间。
    private static String bucketName = "activity-20190118";
	@RequestMapping(value = { "p/alisignature" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult helth(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		ReturnResult returnResult = new ReturnResult();
		
		OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		
		long expireTime = 60*60*1;
		long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
		Date expiration = new Date(expireEndTime);
		PolicyConditions policyConds = new PolicyConditions();
		policyConds.addConditionItem("content-length-range", 0, 1048576000);
		String postPolicy = client.generatePostPolicy(expiration, policyConds);
		byte[] binaryData = postPolicy.getBytes("utf-8");
		String encodedPolicy = BinaryUtil.toBase64String(binaryData);
		String postSignature = client.calculatePostSignature(postPolicy);
		
		
		AliPicRsp aliPicRsp = new AliPicRsp();
		aliPicRsp.setHost(host);
		aliPicRsp.setPolicyBase64(encodedPolicy);
		aliPicRsp.setSignature(postSignature);
		aliPicRsp.setAccessId(accessKeyId);
		
		returnResult.setData(aliPicRsp);
		return returnResult;
	}
	
	
	@RequestMapping(value = { "p/genurl" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult genurl(String fileName,HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		ReturnResult returnResult = new ReturnResult();
		
		OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		
		long expireTime = 60*60*1;
		long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
		Date expiration = new Date(expireEndTime);
		PolicyConditions policyConds = new PolicyConditions();
		policyConds.addConditionItem("content-length-range", 0, 1048576000);
		
		URL filePath = client.generatePresignedUrl(bucketName, fileName, expiration);
		
		returnResult.setData(filePath.toString());
		return returnResult;
	}
}
